package cn.ichiva.puppet.example.curd;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLPath;
import javafx.fxml.Init;
import javafx.fxml.impl.AbstractPuppetComponent;

import java.util.ArrayList;
import java.util.List;

@FXMLPath("department-manager-view.fxml")
public class DepartmentManager extends AbstractPuppetComponent {

    DepartmentDao dao = new DepartmentDao();

    @FXML
    public List<Department> departmentList = dao.findAll();

    @FXML
    StringProperty number = new SimpleStringProperty();

    @FXML
    StringProperty name = new SimpleStringProperty();

    @FXML
    StringProperty superNumber = new SimpleStringProperty();

    @FXML
    StringProperty market = new SimpleStringProperty();

    @FXML
    void add(){
        dao.add(getDepartment());
        departmentList = dao.findAll();
        update();
    }

    private Department getDepartment() {
        Department e = new Department();
        String s = number.get();
        if(null != s)
            e.setId(Long.valueOf(s));

        String name1 = name.get();
        if(null != name1)
            e.setName(name1);

        String s1 = superNumber.get();
        if(s1 != null && s1.length() > 0)
            e.setSuperDept(Long.parseLong(s1));

        String market1 = market.get();
        if(null != market1)
            e.setMarket(market1);
        return e;
    }

    @FXML
    void query(){
        departmentList = dao.find(getDepartment());
        update();
    }
}
